<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/5/11
 * Time: 9:09
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class CommunityModel
 * @package SDXApi\Model
 */
class CommunityModel extends Model
{

    /**
     * @var string
     */
    protected $tableName = 'community';

    /**
     * Created by Jerry
     * @param int $id
     * @return mixed
     */
    public function findCommunityInfo($id = 0)
    {
        $where = array(
            'c.community_id' => $id
        );

        $field = "ca.id as author_id,ca.description as author_sub,ca.articles as article_count,c.community_id,c.content,c.title,c.description,c.reading_times,c.like_times,c.add_time,c.file_url as banner_img,ca.title as author,ca.file_url as head_img";

        return $this->alias("c")
                    ->join("left join sdx_community_author as ca on c.author=ca.id")
                    ->where($where)
                    ->Field($field)
                    ->find();
    }

    /**
     * Created by Jerry
     * @param null $fieldName
     * @param int $id
     * @param int $number
     * @return bool
     */
    public function setCommunityInc($fieldName = null, $id = 0, $number = 1)
    {
        $where = array(
            'community_id'  =>  $id,
        );

        return $this->where($where)
                    ->setInc($fieldName, $number);
    }

    /**
     * Created by Jerry
     * @param $id
     * @return mixed
     */
    public function getAuthorCount($id)
    {
        $where = array(
            'author' => $id,
        );

        return $this->where($where)->count();
    }

    /**
     * @author Jerry
     * @param int $author_id
     * @param int $start
     * @return mixed
     */
    public function findCommunityByAuthorId($author_id = 0, $start = 0)
    {
        $where = array(
            'c.author'  =>  $author_id,
        );

        $field = 'c.community_id,c.title,c.content,c.reading_times,c.like_times,c.add_time,c.file_url as banner_img,c.description,c.description as ios_description';

        return $this->field($field)->alias('c')->where($where)->order('c.add_time desc')->limit($start, 20)->select();
    }

    /**
     * @author Jerry
     * @param array $cat_id_arr
     * @param int $start
     * @return mixed
     */
    public function findCommunityInCatId($cat_id_arr = array(), $start = 0)
    {
        $where = array(
            'c.cat_id'  =>  array('in', $cat_id_arr),
            'c.is_open' =>  1,
        );

        $field = 'ca.id as author_id,c.community_id,c.title,c.reading_times,c.like_times,c.add_time,c.file_url as banner_img,ca.title as author,ca.description as author_sub,ca.file_url as head_img';

        return $this->field($field)
            ->alias('c')
            ->join("left join sdx_community_author as ca on c.author=ca.id")
            ->where($where)
            ->order('rand(), c.article_type desc,c.sort desc,c.add_time desc')
            ->limit($start, 20)
            ->select();
    }

    /**
     * @author Jerry
     * @param $number
     * @return mixed
     */
    public function findCommunityNumberInCatId($number)
    {
        $where = array(
            'c.is_open' =>  1,
        );

        $field = 'ca.id as author_id,c.community_id,c.title,c.reading_times,c.like_times,c.add_time,c.file_url as banner_img,ca.title as author,ca.description as author_sub,ca.file_url as head_img';

        return $this->field($field)
            ->alias('c')
            ->join("left join sdx_community_author as ca on c.author=ca.id")
            ->where($where)
            ->order('rand(), c.article_type desc,c.sort desc,c.add_time desc')
            ->limit($number)
            ->select();
    }

    /**
     * @author Jerry
     * @param int $cat_id
     * @return mixed
     */
    public function getCommunityCountByCatId($cat_id = 0)
    {
        $where = array(
            'cat_id'    =>  $cat_id,
        );

        return $this->where($where)->count();
    }

    /**
     * @author Jerry
     * @return mixed
     */
    public function findCommunity()
    {
        return $this->select();
    }
}